#### "Economic Inequality. Income and political participation in authoritarian regimes" ####
# authors: "Pelke, Lars"
# date: 2020-06-08
# written under "R version 3.6.0 (2019-07-05)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory

# loading packages

library(countrycode)
library(tidyverse)
library(viridis)

#### Import Data ####

# Load VDem data
vdem <- readRDS("data/vdem_10/Country_Year_V-Dem_Full+others_R_v10/V-Dem-CY-Full+Others-v10.rds") 
vdem$iso3n <- countrycode(vdem$country_name, "country.name", "iso3n", warn = TRUE)

vdem$iso3n[vdem$country_name == "Kosovo"] <- 1001
vdem$iso3n[vdem$country_name == "Republic of Vietnam"] <- 704
vdem$iso3n[vdem$country_name == "South Yemen"] <- 720
vdem$iso3n[vdem$country_name == "Zanzibar"] <- 834
  
table(vdem$iso3n)

#Load World value survey data
wvsdata <- readRDS("data/wvs/F00008390-WVS_Longitudinal_1981_2016_r_v20180912.rds")
head(wvsdata)

#Loading Solt SWID DATA##

swiid <- read_csv("data/swiid8_3/swiid8_3_summary.csv") 
swiid$iso3n <- countrycode(swiid$country, "country.name", "iso3n", warn = TRUE)

swiid$iso3n[swiid$country == "Kosovo"] <- 1001
swiid$iso3n[swiid$country == "Yugoslavia"] <-891
swiid$iso3n[swiid$country == "Czechoslovakia"] <- 200
swiid$iso3n[swiid$country == "Micronesia"] <- 583


#### Merge Data ####

## Adding COW Country Codes to the WVS Data to merge with VDEM DATA

# preparing WVS data 
wvsdata <- wvsdata %>%
  dplyr::select(starts_with("S"), A064, A066, A067, A068, A069, A070, A072, 
         A073, A074, A075, A076, A077, A078, A079, A099, A100, A101, 
         A102, A103, A104, A105, A106, A106B, A106C, E264, E257, starts_with("X"))

wvsdata$iso3n <- countrycode(wvsdata$S003, "wvs", "iso3n", warn = TRUE)

wvsdata$iso3n[wvsdata$S003 == 101] <- 70
wvsdata$iso3n[wvsdata$S003 == 914] <- 891

table(wvsdata$iso3n)

wvsdata$year <- wvsdata$S020
wvsdata$year <- as.numeric(wvsdata$year)

# preparing Vdem data
vdem <- vdem %>%
  dplyr::select(country_name,country_id, year, country_text_id, COWcode, iso3n, v2x_polyarchy, v2x_libdem,
         v2x_regime, v2x_regime_amb, v2pepwrses, v2csreprss, starts_with("v2elvot"), v2xel_frefair,  
         v2xnp_client, v2xnp_pres, e_migdppcln, e_migdpgro, e_mipopula, e_miurbpop, 
         starts_with("v2regsupgroups"), v2regimpgroup, v2regsupgroupssize,v2elmulpar, v2elmulpar_osp, v2elmulpar_ord)

# mutate vote buy variable by expanding variable to cases without national elections 
# and to country years without national elections

vdem <- vdem %>% 
  group_by(country_name) %>% 
  fill(v2elvotbuy) # Vote buying fill missing value with last observation, due to data dependence on last elections

vdem <- vdem %>% #code China and Qatar on vote buying=0, because no observations in VDEM and no last national elections
  mutate(v2elvotbuy = ifelse(country_name=="China", 0, v2elvotbuy)) %>%
  mutate(v2elvotbuy = ifelse(country_name=="Qatar", 0, v2elvotbuy))

#### Regime Support group ####

vdem <- vdem %>%
  mutate(regime_support_group_max=pmax(v2regsupgroups_9, v2regsupgroups_10, v2regsupgroups_11, v2regsupgroups_12,  na.rm = TRUE))
# rowmax for urban and rural middle and working class support for the regime 

summary(vdem$regime_support_group_max)

#### Democratic Experience in the the last 20 years ####

vdem <- vdem %>%
  mutate(autocracy= ifelse(v2x_regime<2, 1, 0)) # autocracy == 1, democracy == 0

vdem_auto <- vdem %>%
  filter(year>=1900) %>% # filter years >1900, binary autocracy measures available
  group_by(country_name) %>% 
  mutate(democracy = ifelse(autocracy==1, 0, 1)) %>% # get binary democracy measure for constructing years under democracy
  mutate(autocracy_years = ifelse(autocracy==1, cumsum(autocracy), NA),  # years under autocracy
         democracy_years = ifelse(democracy==1, cumsum(democracy), NA)) %>% # years under democracy
  fill(c(autocracy_years, democracy_years)) %>% # fill NAs with the years under specific regime type
  mutate(democracy_years = ifelse(is.na(democracy_years), 0, democracy_years)) %>%
  ungroup() %>%
  dplyr::select(country_id, year, autocracy_years, democracy_years)

vdem_auto <- vdem_auto %>%
  mutate(democratic_expericene_bi = ifelse(democracy_years >0, 1, 0)) %>%
  dplyr::select(country_id, year, democratic_expericene_bi)

vdem <- vdem %>%
  left_join(vdem_auto, by = c("country_id", "year")) %>%
  mutate(democratic_expericene = ifelse(democratic_expericene_bi==1, max(v2x_libdem), 0)) # Maximal Liberal Democracy 

summary(vdem$democratic_expericene)
summary(vdem$democratic_expericene_bi)

rm(vdem_auto)

#### Filter V-Dem after 1980 ####

vdem <- vdem %>%
  filter(year>1980)

### Democatic Experience since 1980 in all countries as max(v2x_libdem)
vdem <- vdem %>%
  mutate(democratic_expericene_all = max(v2x_libdem))

#### Muliparty elections ####

summary(vdem$v2elmulpar_ord) # NA for non-election years 

vdem <- vdem %>%
  group_by(country_id) %>%
  fill(v2elmulpar_ord) 
summary(vdem$v2elmulpar_ord) # NA for non-election years 


vdem <- vdem %>%
  mutate(v2elmulpar_ord = if_else(is.na(v2elmulpar_ord), 0, v2elmulpar_ord))# replace NA with = 0, assumption: NA no elections in place

summary(vdem$v2elmulpar_ord) 


#### Figure A2 in Supplementary Appendix ####

library(ggpubr)

vdem_fig1 <- vdem %>%
  left_join(swiid, by = c("iso3n", "year")) %>%
  filter(year>=2000) %>%
  mutate(autocracy = case_when(autocracy == 0 ~ "Democracy", 
                               autocracy == 1 ~ "Autocracy"))

ggplot(vdem_fig1, aes(x = gini_disp, group = autocracy, color = as.factor(autocracy))) +
  geom_density(size = 1.1) +
  theme_pubr() +
  scale_color_manual(values=c("#E69F00", "#999999")) +
  scale_fill_manual(values=c("#E69F00", "#999999")) +
    labs(x = "Disposable GINI", 
       y = "Density", 
       color = "", 
       title = "Economic Inequality in Autocracies and Democracies")

ggsave("output/A_density_plot_inequaltiy.pdf", width = 22 , height = 15 , units = c("cm"))


rm(vdem_fig1)

#### Mergigung with V-DEM DATA ####

wvsdata <- wvsdata %>%
  left_join(vdem, c("iso3n", "year"))

head(wvsdata)

# Drop observations without year or country code
wvsdata <- wvsdata %>%
  drop_na(iso3n, year)

#### Merging with SWIID DATA ####
wvsdata <- wvsdata %>%
  left_join(swiid, c("iso3n", "year"))

# Drop observations without year or country code
wvsdata <- wvsdata %>%
  drop_na(iso3n, year)

#### Identify sample and waves ####
sample <- wvsdata %>%
  filter(v2x_regime_amb<=4) %>%
  distinct(country_name,year, .keep_all = TRUE)
table(sample$country_name)

rm(sample)

#### Final steps ####

# export basic dataset 
write_rds(wvsdata, "data/wvsdata.rds")



